package com.scoy.honeymei.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.scoy.honeymei.adapter.tabadapter.OyAdapter;
import com.scoy.honeymei.bean.FilmBean;
import com.scoy.honeymei.databinding.ItemFilmnewHBinding;
import com.scoy.honeymei.databinding.ItemFilmnewVBinding;
import com.scoy.honeymei.utils.GlideImgUtil;

import org.jetbrains.annotations.NotNull;

public class FilmNewHListAdapter extends OyAdapter<FilmBean> {


    private int type;

    public FilmNewHListAdapter(Context context) {
        super(context);
    }

    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            ItemFilmnewVBinding binding1 = ItemFilmnewVBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new Oy1Holder(binding1);
        } else {
            ItemFilmnewHBinding binding = ItemFilmnewHBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new OyHolder(binding);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {

        FilmBean mBean = datalist.get(position);
        //type 商品排列的方式 0：网格；1：垂直列表排列
        if (type == 1) {
            Oy1Holder mHolder1 = (Oy1Holder) holder;

            mHolder1.binding.ifnhNameTv.setText(mBean.getTitle());
            GlideImgUtil.glidePicNo(context, mBean.getImage(), mHolder1.binding.ifnhIv);
            mHolder1.binding.ifnhPriceTv.setText(mBean.getPrice());

            mHolder1.binding.ifnhShareIv.setOnClickListener(v -> {
                if (onTwoClick != null) {
                    onTwoClick.twoClick(position);
                }
            });
        } else {
            OyHolder mHolder = (OyHolder) holder;
            mHolder.binding.ifnhNameTv.setText(mBean.getTitle());
            GlideImgUtil.glidePicNo(context, mBean.getImage(), mHolder.binding.ifnhIv);
            mHolder.binding.ifnhPriceTv.setText(mBean.getPrice());

            mHolder.binding.ifnhShareIv.setOnClickListener(v -> {
                if (onTwoClick != null) {
                    onTwoClick.twoClick(position);
                }
            });


        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onOneClick != null) {
                    onOneClick.oneClick(position);
                }
            }
        });

    }

    /**
     * 点击切换布局的时候调用这个方法设置type
     *
     * @param type 商品排列的方式 0：网格；1：垂直列表排列
     */
    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getItemViewType(int position) {
        return type;
    }


    static class OyHolder extends RecyclerView.ViewHolder {
        private ItemFilmnewHBinding binding;

        public OyHolder(ItemFilmnewHBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }


    static class Oy1Holder extends RecyclerView.ViewHolder {
        private ItemFilmnewVBinding binding;

        public Oy1Holder(ItemFilmnewVBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
